package com.xbongbong.crm.handle.analysis;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.BusinessUserTeamHelp;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.parent.parse.data.AbstractListAnalysisDataService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.FormConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.help.FormDataAnalysisHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;

/**
 * 用于特殊处理客户的解析方法
 * @author 吴峰
 * @date 2021/12/21 10:58
 */
@Service("customerListAnalysisDataHandler")
public class CustomerListAnalysisDataHandler extends AbstractListAnalysisDataService<PaasFormDataEntityExt> {


    @Resource
    private BusinessUserTeamHelp businessUserTeamHelp;

    @Override
    public List<Integer> businessList() {
        return Arrays.asList(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
    }

    /**
     * 解析特殊数据，地址，附件，定位，多选转成逗号隔开的字符串
     * @param resultJsonObject
     */
    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject,
                             Map<String, Object> linkData, Set<Object> linkDataSet) throws XbbException {
        boolean customerLastConnectTime = true;
        // 客户最后跟进时间格式标识
        if(Objects.nonNull(resultJsonObject.get(FormConfigEnum.CUSTOMER_LAST_CONNECT_TIME.getDbAlias()))){
            String lastConnectTime = resultJsonObject.get(FormConfigEnum.CUSTOMER_LAST_CONNECT_TIME.getDbAlias()).toString();
            customerLastConnectTime = Objects.equals(lastConnectTime, "1");
        }

        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        String key = analysisDataDTO.getAttr();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();
        Integer businessType = analysisDataDTO.getBusinessType();

        // 判断高级规则
        // 审批摘要不判断高级权限
        if (Objects.isNull(analysisDataDTO.getIsProcess()) || !analysisDataDTO.getIsProcess()) {
            FormDataAnalysisHelp.invisibleSetNull(fieldAttrEntity, analysisDataDTO, objectJsonObject);
        }
        //
        boolean isTrue = (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode())
                && Objects.equals(fieldAttrEntity.getSaasAttr(), CustomerManagementEnum.LAST_CONNECT_TIME.getSaasAttr()) && customerLastConnectTime);
        if(fieldAttrEntity.getFieldType().equals(FieldTypeEnum.DATETIME.getType()) && isTrue){
            // 客户的最后跟进时间特殊处理
            resultJsonObject.put(key, DateTimeUtil.getLastConnectTimeStr(objectJsonObject.getLong(key)));
        } else {
            super.analysisData(analysisDataDTO, resultJsonObject, linkData, linkDataSet);
        }
    }

    @Override
    protected Map<Long, List<String>>  getUserTeamMap(Set<String> userIdList, List<Long> dataIdList, String corpid, Integer businessType, Map<Long, List<String>> mainUserIdMap,
                                                      Map<Long, List<String>> coUserIdMap, Integer subBusinessType) throws XbbException {
        Boolean isPublic = Objects.equals(XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode(),subBusinessType);
        businessUserTeamHelp.getUserIdMapSetShow(userIdList,dataIdList,corpid,businessType,isPublic,mainUserIdMap, coUserIdMap, DelEnum.NORMAL.getDel());
        return new HashMap<>(BasicConstant.ZERO);
    }

    @Override
    protected boolean publicIsVisible(JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (!Objects.equals(FieldTypeEnum.CONTACT_NUMBER.getType(), fieldAttrEntity.getFieldType())) {
            return false;
        }
        Integer isPublic = getIntegerOrDefaultFromFormData(data, CustomerManagementEnum.IS_PUBLIC.getAttr(), BasicConstant.ZERO);

        Integer publicPool = fieldAttrEntity.getPublicPool();
        Integer showPublicPool = fieldAttrEntity.getShowPublicPool();
        return Objects.equals(isPublic, BasicConstant.ONE) && Objects.equals(showPublicPool, BasicConstant.ONE) && Objects.equals(publicPool, BasicConstant.ZERO);
    }
}
